{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "f7cfdb70-2671-404c-8bcf-2584c661a7b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入数据分析和计算模块\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "74a91621-60c0-48e4-9c19-39b3135b6a2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "data_1 = pd.read_csv(\"D:/Users/19202/Desktop/world_happiness_report.csv\")\n",
    "data_2 = pd.read_csv(\"D:/Users/19202/Desktop/world_happiness_report_2021.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "73386f65-98a7-4f91-a917-2f9911a5019e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country name</th>\n",
       "      <th>year</th>\n",
       "      <th>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2008</td>\n",
       "      <td>3.724</td>\n",
       "      <td>7.370</td>\n",
       "      <td>0.451</td>\n",
       "      <td>50.80</td>\n",
       "      <td>0.718</td>\n",
       "      <td>0.168</td>\n",
       "      <td>0.882</td>\n",
       "      <td>0.518</td>\n",
       "      <td>0.258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2009</td>\n",
       "      <td>4.402</td>\n",
       "      <td>7.540</td>\n",
       "      <td>0.552</td>\n",
       "      <td>51.20</td>\n",
       "      <td>0.679</td>\n",
       "      <td>0.190</td>\n",
       "      <td>0.850</td>\n",
       "      <td>0.584</td>\n",
       "      <td>0.237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2010</td>\n",
       "      <td>4.758</td>\n",
       "      <td>7.647</td>\n",
       "      <td>0.539</td>\n",
       "      <td>51.60</td>\n",
       "      <td>0.600</td>\n",
       "      <td>0.121</td>\n",
       "      <td>0.707</td>\n",
       "      <td>0.618</td>\n",
       "      <td>0.275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2011</td>\n",
       "      <td>3.832</td>\n",
       "      <td>7.620</td>\n",
       "      <td>0.521</td>\n",
       "      <td>51.92</td>\n",
       "      <td>0.496</td>\n",
       "      <td>0.162</td>\n",
       "      <td>0.731</td>\n",
       "      <td>0.611</td>\n",
       "      <td>0.267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2012</td>\n",
       "      <td>3.783</td>\n",
       "      <td>7.705</td>\n",
       "      <td>0.521</td>\n",
       "      <td>52.24</td>\n",
       "      <td>0.531</td>\n",
       "      <td>0.236</td>\n",
       "      <td>0.776</td>\n",
       "      <td>0.710</td>\n",
       "      <td>0.268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1944</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2016</td>\n",
       "      <td>3.735</td>\n",
       "      <td>7.984</td>\n",
       "      <td>0.768</td>\n",
       "      <td>54.40</td>\n",
       "      <td>0.733</td>\n",
       "      <td>-0.095</td>\n",
       "      <td>0.724</td>\n",
       "      <td>0.738</td>\n",
       "      <td>0.209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1945</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2017</td>\n",
       "      <td>3.638</td>\n",
       "      <td>8.016</td>\n",
       "      <td>0.754</td>\n",
       "      <td>55.00</td>\n",
       "      <td>0.753</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.751</td>\n",
       "      <td>0.806</td>\n",
       "      <td>0.224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1946</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2018</td>\n",
       "      <td>3.616</td>\n",
       "      <td>8.049</td>\n",
       "      <td>0.775</td>\n",
       "      <td>55.60</td>\n",
       "      <td>0.763</td>\n",
       "      <td>-0.068</td>\n",
       "      <td>0.844</td>\n",
       "      <td>0.710</td>\n",
       "      <td>0.212</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1947</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2019</td>\n",
       "      <td>2.694</td>\n",
       "      <td>7.950</td>\n",
       "      <td>0.759</td>\n",
       "      <td>56.20</td>\n",
       "      <td>0.632</td>\n",
       "      <td>-0.064</td>\n",
       "      <td>0.831</td>\n",
       "      <td>0.716</td>\n",
       "      <td>0.235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1948</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2020</td>\n",
       "      <td>3.160</td>\n",
       "      <td>7.829</td>\n",
       "      <td>0.717</td>\n",
       "      <td>56.80</td>\n",
       "      <td>0.643</td>\n",
       "      <td>-0.009</td>\n",
       "      <td>0.789</td>\n",
       "      <td>0.703</td>\n",
       "      <td>0.346</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1949 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Country name  year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "0     Afghanistan  2008        3.724               7.370           0.451   \n",
       "1     Afghanistan  2009        4.402               7.540           0.552   \n",
       "2     Afghanistan  2010        4.758               7.647           0.539   \n",
       "3     Afghanistan  2011        3.832               7.620           0.521   \n",
       "4     Afghanistan  2012        3.783               7.705           0.521   \n",
       "...           ...   ...          ...                 ...             ...   \n",
       "1944     Zimbabwe  2016        3.735               7.984           0.768   \n",
       "1945     Zimbabwe  2017        3.638               8.016           0.754   \n",
       "1946     Zimbabwe  2018        3.616               8.049           0.775   \n",
       "1947     Zimbabwe  2019        2.694               7.950           0.759   \n",
       "1948     Zimbabwe  2020        3.160               7.829           0.717   \n",
       "\n",
       "      Healthy life expectancy at birth  Freedom to make life choices  \\\n",
       "0                                50.80                         0.718   \n",
       "1                                51.20                         0.679   \n",
       "2                                51.60                         0.600   \n",
       "3                                51.92                         0.496   \n",
       "4                                52.24                         0.531   \n",
       "...                                ...                           ...   \n",
       "1944                             54.40                         0.733   \n",
       "1945                             55.00                         0.753   \n",
       "1946                             55.60                         0.763   \n",
       "1947                             56.20                         0.632   \n",
       "1948                             56.80                         0.643   \n",
       "\n",
       "      Generosity  Perceptions of corruption  Positive affect  Negative affect  \n",
       "0          0.168                      0.882            0.518            0.258  \n",
       "1          0.190                      0.850            0.584            0.237  \n",
       "2          0.121                      0.707            0.618            0.275  \n",
       "3          0.162                      0.731            0.611            0.267  \n",
       "4          0.236                      0.776            0.710            0.268  \n",
       "...          ...                        ...              ...              ...  \n",
       "1944      -0.095                      0.724            0.738            0.209  \n",
       "1945      -0.098                      0.751            0.806            0.224  \n",
       "1946      -0.068                      0.844            0.710            0.212  \n",
       "1947      -0.064                      0.831            0.716            0.235  \n",
       "1948      -0.009                      0.789            0.703            0.346  \n",
       "\n",
       "[1949 rows x 11 columns]"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "6de073a2-77bb-4e3c-adbc-fd69bbb0bd63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country name</th>\n",
       "      <th>Regional indicator</th>\n",
       "      <th>Ladder score</th>\n",
       "      <th>Standard error of ladder score</th>\n",
       "      <th>upperwhisker</th>\n",
       "      <th>lowerwhisker</th>\n",
       "      <th>Logged GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Ladder score in Dystopia</th>\n",
       "      <th>Explained by: Log GDP per capita</th>\n",
       "      <th>Explained by: Social support</th>\n",
       "      <th>Explained by: Healthy life expectancy</th>\n",
       "      <th>Explained by: Freedom to make life choices</th>\n",
       "      <th>Explained by: Generosity</th>\n",
       "      <th>Explained by: Perceptions of corruption</th>\n",
       "      <th>Dystopia + residual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Finland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.842</td>\n",
       "      <td>0.032</td>\n",
       "      <td>7.904</td>\n",
       "      <td>7.780</td>\n",
       "      <td>10.775</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.000</td>\n",
       "      <td>0.949</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.186</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.446</td>\n",
       "      <td>1.106</td>\n",
       "      <td>0.741</td>\n",
       "      <td>0.691</td>\n",
       "      <td>0.124</td>\n",
       "      <td>0.481</td>\n",
       "      <td>3.253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.620</td>\n",
       "      <td>0.035</td>\n",
       "      <td>7.687</td>\n",
       "      <td>7.552</td>\n",
       "      <td>10.933</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.700</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.179</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.502</td>\n",
       "      <td>1.108</td>\n",
       "      <td>0.763</td>\n",
       "      <td>0.686</td>\n",
       "      <td>0.208</td>\n",
       "      <td>0.485</td>\n",
       "      <td>2.868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.571</td>\n",
       "      <td>0.036</td>\n",
       "      <td>7.643</td>\n",
       "      <td>7.500</td>\n",
       "      <td>11.117</td>\n",
       "      <td>0.942</td>\n",
       "      <td>74.400</td>\n",
       "      <td>0.919</td>\n",
       "      <td>0.025</td>\n",
       "      <td>0.292</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.566</td>\n",
       "      <td>1.079</td>\n",
       "      <td>0.816</td>\n",
       "      <td>0.653</td>\n",
       "      <td>0.204</td>\n",
       "      <td>0.413</td>\n",
       "      <td>2.839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Iceland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.554</td>\n",
       "      <td>0.059</td>\n",
       "      <td>7.670</td>\n",
       "      <td>7.438</td>\n",
       "      <td>10.878</td>\n",
       "      <td>0.983</td>\n",
       "      <td>73.000</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0.160</td>\n",
       "      <td>0.673</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.482</td>\n",
       "      <td>1.172</td>\n",
       "      <td>0.772</td>\n",
       "      <td>0.698</td>\n",
       "      <td>0.293</td>\n",
       "      <td>0.170</td>\n",
       "      <td>2.967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.464</td>\n",
       "      <td>0.027</td>\n",
       "      <td>7.518</td>\n",
       "      <td>7.410</td>\n",
       "      <td>10.932</td>\n",
       "      <td>0.942</td>\n",
       "      <td>72.400</td>\n",
       "      <td>0.913</td>\n",
       "      <td>0.175</td>\n",
       "      <td>0.338</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.501</td>\n",
       "      <td>1.079</td>\n",
       "      <td>0.753</td>\n",
       "      <td>0.647</td>\n",
       "      <td>0.302</td>\n",
       "      <td>0.384</td>\n",
       "      <td>2.798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>Lesotho</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>3.512</td>\n",
       "      <td>0.120</td>\n",
       "      <td>3.748</td>\n",
       "      <td>3.276</td>\n",
       "      <td>7.926</td>\n",
       "      <td>0.787</td>\n",
       "      <td>48.700</td>\n",
       "      <td>0.715</td>\n",
       "      <td>-0.131</td>\n",
       "      <td>0.915</td>\n",
       "      <td>2.43</td>\n",
       "      <td>0.451</td>\n",
       "      <td>0.731</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.405</td>\n",
       "      <td>0.103</td>\n",
       "      <td>0.015</td>\n",
       "      <td>1.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>Botswana</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>3.467</td>\n",
       "      <td>0.074</td>\n",
       "      <td>3.611</td>\n",
       "      <td>3.322</td>\n",
       "      <td>9.782</td>\n",
       "      <td>0.784</td>\n",
       "      <td>59.269</td>\n",
       "      <td>0.824</td>\n",
       "      <td>-0.246</td>\n",
       "      <td>0.801</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.099</td>\n",
       "      <td>0.724</td>\n",
       "      <td>0.340</td>\n",
       "      <td>0.539</td>\n",
       "      <td>0.027</td>\n",
       "      <td>0.088</td>\n",
       "      <td>0.648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>Rwanda</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>3.415</td>\n",
       "      <td>0.068</td>\n",
       "      <td>3.548</td>\n",
       "      <td>3.282</td>\n",
       "      <td>7.676</td>\n",
       "      <td>0.552</td>\n",
       "      <td>61.400</td>\n",
       "      <td>0.897</td>\n",
       "      <td>0.061</td>\n",
       "      <td>0.167</td>\n",
       "      <td>2.43</td>\n",
       "      <td>0.364</td>\n",
       "      <td>0.202</td>\n",
       "      <td>0.407</td>\n",
       "      <td>0.627</td>\n",
       "      <td>0.227</td>\n",
       "      <td>0.493</td>\n",
       "      <td>1.095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>3.145</td>\n",
       "      <td>0.058</td>\n",
       "      <td>3.259</td>\n",
       "      <td>3.030</td>\n",
       "      <td>7.943</td>\n",
       "      <td>0.750</td>\n",
       "      <td>56.201</td>\n",
       "      <td>0.677</td>\n",
       "      <td>-0.047</td>\n",
       "      <td>0.821</td>\n",
       "      <td>2.43</td>\n",
       "      <td>0.457</td>\n",
       "      <td>0.649</td>\n",
       "      <td>0.243</td>\n",
       "      <td>0.359</td>\n",
       "      <td>0.157</td>\n",
       "      <td>0.075</td>\n",
       "      <td>1.205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>South Asia</td>\n",
       "      <td>2.523</td>\n",
       "      <td>0.038</td>\n",
       "      <td>2.596</td>\n",
       "      <td>2.449</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.463</td>\n",
       "      <td>52.493</td>\n",
       "      <td>0.382</td>\n",
       "      <td>-0.102</td>\n",
       "      <td>0.924</td>\n",
       "      <td>2.43</td>\n",
       "      <td>0.370</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.126</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.122</td>\n",
       "      <td>0.010</td>\n",
       "      <td>1.895</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>149 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Country name  Regional indicator  Ladder score  \\\n",
       "0        Finland      Western Europe         7.842   \n",
       "1        Denmark      Western Europe         7.620   \n",
       "2    Switzerland      Western Europe         7.571   \n",
       "3        Iceland      Western Europe         7.554   \n",
       "4    Netherlands      Western Europe         7.464   \n",
       "..           ...                 ...           ...   \n",
       "144      Lesotho  Sub-Saharan Africa         3.512   \n",
       "145     Botswana  Sub-Saharan Africa         3.467   \n",
       "146       Rwanda  Sub-Saharan Africa         3.415   \n",
       "147     Zimbabwe  Sub-Saharan Africa         3.145   \n",
       "148  Afghanistan          South Asia         2.523   \n",
       "\n",
       "     Standard error of ladder score  upperwhisker  lowerwhisker  \\\n",
       "0                             0.032         7.904         7.780   \n",
       "1                             0.035         7.687         7.552   \n",
       "2                             0.036         7.643         7.500   \n",
       "3                             0.059         7.670         7.438   \n",
       "4                             0.027         7.518         7.410   \n",
       "..                              ...           ...           ...   \n",
       "144                           0.120         3.748         3.276   \n",
       "145                           0.074         3.611         3.322   \n",
       "146                           0.068         3.548         3.282   \n",
       "147                           0.058         3.259         3.030   \n",
       "148                           0.038         2.596         2.449   \n",
       "\n",
       "     Logged GDP per capita  Social support  Healthy life expectancy  \\\n",
       "0                   10.775           0.954                   72.000   \n",
       "1                   10.933           0.954                   72.700   \n",
       "2                   11.117           0.942                   74.400   \n",
       "3                   10.878           0.983                   73.000   \n",
       "4                   10.932           0.942                   72.400   \n",
       "..                     ...             ...                      ...   \n",
       "144                  7.926           0.787                   48.700   \n",
       "145                  9.782           0.784                   59.269   \n",
       "146                  7.676           0.552                   61.400   \n",
       "147                  7.943           0.750                   56.201   \n",
       "148                  7.695           0.463                   52.493   \n",
       "\n",
       "     Freedom to make life choices  Generosity  Perceptions of corruption  \\\n",
       "0                           0.949      -0.098                      0.186   \n",
       "1                           0.946       0.030                      0.179   \n",
       "2                           0.919       0.025                      0.292   \n",
       "3                           0.955       0.160                      0.673   \n",
       "4                           0.913       0.175                      0.338   \n",
       "..                            ...         ...                        ...   \n",
       "144                         0.715      -0.131                      0.915   \n",
       "145                         0.824      -0.246                      0.801   \n",
       "146                         0.897       0.061                      0.167   \n",
       "147                         0.677      -0.047                      0.821   \n",
       "148                         0.382      -0.102                      0.924   \n",
       "\n",
       "     Ladder score in Dystopia  Explained by: Log GDP per capita  \\\n",
       "0                        2.43                             1.446   \n",
       "1                        2.43                             1.502   \n",
       "2                        2.43                             1.566   \n",
       "3                        2.43                             1.482   \n",
       "4                        2.43                             1.501   \n",
       "..                        ...                               ...   \n",
       "144                      2.43                             0.451   \n",
       "145                      2.43                             1.099   \n",
       "146                      2.43                             0.364   \n",
       "147                      2.43                             0.457   \n",
       "148                      2.43                             0.370   \n",
       "\n",
       "     Explained by: Social support  Explained by: Healthy life expectancy  \\\n",
       "0                           1.106                                  0.741   \n",
       "1                           1.108                                  0.763   \n",
       "2                           1.079                                  0.816   \n",
       "3                           1.172                                  0.772   \n",
       "4                           1.079                                  0.753   \n",
       "..                            ...                                    ...   \n",
       "144                         0.731                                  0.007   \n",
       "145                         0.724                                  0.340   \n",
       "146                         0.202                                  0.407   \n",
       "147                         0.649                                  0.243   \n",
       "148                         0.000                                  0.126   \n",
       "\n",
       "     Explained by: Freedom to make life choices  Explained by: Generosity  \\\n",
       "0                                         0.691                     0.124   \n",
       "1                                         0.686                     0.208   \n",
       "2                                         0.653                     0.204   \n",
       "3                                         0.698                     0.293   \n",
       "4                                         0.647                     0.302   \n",
       "..                                          ...                       ...   \n",
       "144                                       0.405                     0.103   \n",
       "145                                       0.539                     0.027   \n",
       "146                                       0.627                     0.227   \n",
       "147                                       0.359                     0.157   \n",
       "148                                       0.000                     0.122   \n",
       "\n",
       "     Explained by: Perceptions of corruption  Dystopia + residual  \n",
       "0                                      0.481                3.253  \n",
       "1                                      0.485                2.868  \n",
       "2                                      0.413                2.839  \n",
       "3                                      0.170                2.967  \n",
       "4                                      0.384                2.798  \n",
       "..                                       ...                  ...  \n",
       "144                                    0.015                1.800  \n",
       "145                                    0.088                0.648  \n",
       "146                                    0.493                1.095  \n",
       "147                                    0.075                1.205  \n",
       "148                                    0.010                1.895  \n",
       "\n",
       "[149 rows x 20 columns]"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "2b7c6e76-bd76-4f12-a40a-01b87a1934cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列名汉化\n",
    "data_1.columns = ['国家名称', '年', '阶梯分数', '人均GDP', '社会支持', '健康预期寿命', '做出人生选择的自由', '慷慨', '对腐败的看法', '积极情感', '消极情感']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "4b871bb3-460b-4dbd-b22a-97d9d7d1c7a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列名汉化\n",
    "data_2.columns = ['国家名称', '地区指标', '阶梯分数', '阶梯分数标准误差', '上须', '下须', '人均GDP', '社会支持', '健康预期寿命', '做出人生选择的自由', '慷慨', '对腐败的看法', '反乌托邦的阶梯分数', '解释:对数人均GDP', '解释:社会支持', '解释:健康预期寿命', '解释者:做出人生选择的自由', '解释者:慷慨', '解释者:对腐败的看法', '反乌托邦+残余']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "82810187-c878-496c-b72b-dd37f61783fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1949 entries, 0 to 1948\n",
      "Data columns (total 11 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   国家名称       1949 non-null   object \n",
      " 1   年          1949 non-null   int64  \n",
      " 2   阶梯分数       1949 non-null   float64\n",
      " 3   人均GDP      1913 non-null   float64\n",
      " 4   社会支持       1936 non-null   float64\n",
      " 5   健康预期寿命     1894 non-null   float64\n",
      " 6   做出人生选择的自由  1917 non-null   float64\n",
      " 7   慷慨         1860 non-null   float64\n",
      " 8   对腐败的看法     1839 non-null   float64\n",
      " 9   积极情感       1927 non-null   float64\n",
      " 10  消极情感       1933 non-null   float64\n",
      "dtypes: float64(9), int64(1), object(1)\n",
      "memory usage: 167.6+ KB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>年</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "      <th>积极情感</th>\n",
       "      <th>消极情感</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2008</td>\n",
       "      <td>3.724</td>\n",
       "      <td>7.370</td>\n",
       "      <td>0.451</td>\n",
       "      <td>50.80</td>\n",
       "      <td>0.718</td>\n",
       "      <td>0.168</td>\n",
       "      <td>0.882</td>\n",
       "      <td>0.518</td>\n",
       "      <td>0.258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2009</td>\n",
       "      <td>4.402</td>\n",
       "      <td>7.540</td>\n",
       "      <td>0.552</td>\n",
       "      <td>51.20</td>\n",
       "      <td>0.679</td>\n",
       "      <td>0.190</td>\n",
       "      <td>0.850</td>\n",
       "      <td>0.584</td>\n",
       "      <td>0.237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2010</td>\n",
       "      <td>4.758</td>\n",
       "      <td>7.647</td>\n",
       "      <td>0.539</td>\n",
       "      <td>51.60</td>\n",
       "      <td>0.600</td>\n",
       "      <td>0.121</td>\n",
       "      <td>0.707</td>\n",
       "      <td>0.618</td>\n",
       "      <td>0.275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2011</td>\n",
       "      <td>3.832</td>\n",
       "      <td>7.620</td>\n",
       "      <td>0.521</td>\n",
       "      <td>51.92</td>\n",
       "      <td>0.496</td>\n",
       "      <td>0.162</td>\n",
       "      <td>0.731</td>\n",
       "      <td>0.611</td>\n",
       "      <td>0.267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2012</td>\n",
       "      <td>3.783</td>\n",
       "      <td>7.705</td>\n",
       "      <td>0.521</td>\n",
       "      <td>52.24</td>\n",
       "      <td>0.531</td>\n",
       "      <td>0.236</td>\n",
       "      <td>0.776</td>\n",
       "      <td>0.710</td>\n",
       "      <td>0.268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          国家名称     年   阶梯分数  人均GDP   社会支持  健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法  \\\n",
       "0  Afghanistan  2008  3.724  7.370  0.451   50.80      0.718  0.168   0.882   \n",
       "1  Afghanistan  2009  4.402  7.540  0.552   51.20      0.679  0.190   0.850   \n",
       "2  Afghanistan  2010  4.758  7.647  0.539   51.60      0.600  0.121   0.707   \n",
       "3  Afghanistan  2011  3.832  7.620  0.521   51.92      0.496  0.162   0.731   \n",
       "4  Afghanistan  2012  3.783  7.705  0.521   52.24      0.531  0.236   0.776   \n",
       "\n",
       "    积极情感   消极情感  \n",
       "0  0.518  0.258  \n",
       "1  0.584  0.237  \n",
       "2  0.618  0.275  \n",
       "3  0.611  0.267  \n",
       "4  0.710  0.268  "
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据探查\n",
    "data_1.info()\n",
    "data_1.head(5)  # 查看前5条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "36a24aa8-6539-42f0-84f7-cd844fc10402",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 149 entries, 0 to 148\n",
      "Data columns (total 20 columns):\n",
      " #   Column         Non-Null Count  Dtype  \n",
      "---  ------         --------------  -----  \n",
      " 0   国家名称           149 non-null    object \n",
      " 1   地区指标           149 non-null    object \n",
      " 2   阶梯分数           149 non-null    float64\n",
      " 3   阶梯分数标准误差       149 non-null    float64\n",
      " 4   上须             149 non-null    float64\n",
      " 5   下须             149 non-null    float64\n",
      " 6   人均GDP          149 non-null    float64\n",
      " 7   社会支持           149 non-null    float64\n",
      " 8   健康预期寿命         149 non-null    float64\n",
      " 9   做出人生选择的自由      149 non-null    float64\n",
      " 10  慷慨             149 non-null    float64\n",
      " 11  对腐败的看法         149 non-null    float64\n",
      " 12  反乌托邦的阶梯分数      149 non-null    float64\n",
      " 13  解释:对数人均GDP     149 non-null    float64\n",
      " 14  解释:社会支持        149 non-null    float64\n",
      " 15  解释:健康预期寿命      149 non-null    float64\n",
      " 16  解释者:做出人生选择的自由  149 non-null    float64\n",
      " 17  解释者:慷慨         149 non-null    float64\n",
      " 18  解释者:对腐败的看法     149 non-null    float64\n",
      " 19  反乌托邦+残余        149 non-null    float64\n",
      "dtypes: float64(18), object(2)\n",
      "memory usage: 23.4+ KB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>地区指标</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>阶梯分数标准误差</th>\n",
       "      <th>上须</th>\n",
       "      <th>下须</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "      <th>反乌托邦的阶梯分数</th>\n",
       "      <th>解释:对数人均GDP</th>\n",
       "      <th>解释:社会支持</th>\n",
       "      <th>解释:健康预期寿命</th>\n",
       "      <th>解释者:做出人生选择的自由</th>\n",
       "      <th>解释者:慷慨</th>\n",
       "      <th>解释者:对腐败的看法</th>\n",
       "      <th>反乌托邦+残余</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Finland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.842</td>\n",
       "      <td>0.032</td>\n",
       "      <td>7.904</td>\n",
       "      <td>7.780</td>\n",
       "      <td>10.775</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.0</td>\n",
       "      <td>0.949</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.186</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.446</td>\n",
       "      <td>1.106</td>\n",
       "      <td>0.741</td>\n",
       "      <td>0.691</td>\n",
       "      <td>0.124</td>\n",
       "      <td>0.481</td>\n",
       "      <td>3.253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.620</td>\n",
       "      <td>0.035</td>\n",
       "      <td>7.687</td>\n",
       "      <td>7.552</td>\n",
       "      <td>10.933</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.7</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.179</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.502</td>\n",
       "      <td>1.108</td>\n",
       "      <td>0.763</td>\n",
       "      <td>0.686</td>\n",
       "      <td>0.208</td>\n",
       "      <td>0.485</td>\n",
       "      <td>2.868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.571</td>\n",
       "      <td>0.036</td>\n",
       "      <td>7.643</td>\n",
       "      <td>7.500</td>\n",
       "      <td>11.117</td>\n",
       "      <td>0.942</td>\n",
       "      <td>74.4</td>\n",
       "      <td>0.919</td>\n",
       "      <td>0.025</td>\n",
       "      <td>0.292</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.566</td>\n",
       "      <td>1.079</td>\n",
       "      <td>0.816</td>\n",
       "      <td>0.653</td>\n",
       "      <td>0.204</td>\n",
       "      <td>0.413</td>\n",
       "      <td>2.839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Iceland</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.554</td>\n",
       "      <td>0.059</td>\n",
       "      <td>7.670</td>\n",
       "      <td>7.438</td>\n",
       "      <td>10.878</td>\n",
       "      <td>0.983</td>\n",
       "      <td>73.0</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0.160</td>\n",
       "      <td>0.673</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.482</td>\n",
       "      <td>1.172</td>\n",
       "      <td>0.772</td>\n",
       "      <td>0.698</td>\n",
       "      <td>0.293</td>\n",
       "      <td>0.170</td>\n",
       "      <td>2.967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>Western Europe</td>\n",
       "      <td>7.464</td>\n",
       "      <td>0.027</td>\n",
       "      <td>7.518</td>\n",
       "      <td>7.410</td>\n",
       "      <td>10.932</td>\n",
       "      <td>0.942</td>\n",
       "      <td>72.4</td>\n",
       "      <td>0.913</td>\n",
       "      <td>0.175</td>\n",
       "      <td>0.338</td>\n",
       "      <td>2.43</td>\n",
       "      <td>1.501</td>\n",
       "      <td>1.079</td>\n",
       "      <td>0.753</td>\n",
       "      <td>0.647</td>\n",
       "      <td>0.302</td>\n",
       "      <td>0.384</td>\n",
       "      <td>2.798</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          国家名称            地区指标   阶梯分数  阶梯分数标准误差     上须     下须   人均GDP   社会支持  \\\n",
       "0      Finland  Western Europe  7.842     0.032  7.904  7.780  10.775  0.954   \n",
       "1      Denmark  Western Europe  7.620     0.035  7.687  7.552  10.933  0.954   \n",
       "2  Switzerland  Western Europe  7.571     0.036  7.643  7.500  11.117  0.942   \n",
       "3      Iceland  Western Europe  7.554     0.059  7.670  7.438  10.878  0.983   \n",
       "4  Netherlands  Western Europe  7.464     0.027  7.518  7.410  10.932  0.942   \n",
       "\n",
       "   健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法  反乌托邦的阶梯分数  解释:对数人均GDP  解释:社会支持  \\\n",
       "0    72.0      0.949 -0.098   0.186       2.43       1.446    1.106   \n",
       "1    72.7      0.946  0.030   0.179       2.43       1.502    1.108   \n",
       "2    74.4      0.919  0.025   0.292       2.43       1.566    1.079   \n",
       "3    73.0      0.955  0.160   0.673       2.43       1.482    1.172   \n",
       "4    72.4      0.913  0.175   0.338       2.43       1.501    1.079   \n",
       "\n",
       "   解释:健康预期寿命  解释者:做出人生选择的自由  解释者:慷慨  解释者:对腐败的看法  反乌托邦+残余  \n",
       "0      0.741          0.691   0.124       0.481    3.253  \n",
       "1      0.763          0.686   0.208       0.485    2.868  \n",
       "2      0.816          0.653   0.204       0.413    2.839  \n",
       "3      0.772          0.698   0.293       0.170    2.967  \n",
       "4      0.753          0.647   0.302       0.384    2.798  "
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据探查\n",
    "data_2.info()\n",
    "data_2.head()  # head()默认不写，也是显示前5条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "0ad3fead-7f4d-4e42-b6c7-ad3563851861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Finland</td>\n",
       "      <td>7.842</td>\n",
       "      <td>10.775</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.0</td>\n",
       "      <td>0.949</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>7.620</td>\n",
       "      <td>10.933</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.7</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Switzerland</td>\n",
       "      <td>7.571</td>\n",
       "      <td>11.117</td>\n",
       "      <td>0.942</td>\n",
       "      <td>74.4</td>\n",
       "      <td>0.919</td>\n",
       "      <td>0.025</td>\n",
       "      <td>0.292</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Iceland</td>\n",
       "      <td>7.554</td>\n",
       "      <td>10.878</td>\n",
       "      <td>0.983</td>\n",
       "      <td>73.0</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0.160</td>\n",
       "      <td>0.673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>7.464</td>\n",
       "      <td>10.932</td>\n",
       "      <td>0.942</td>\n",
       "      <td>72.4</td>\n",
       "      <td>0.913</td>\n",
       "      <td>0.175</td>\n",
       "      <td>0.338</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          国家名称   阶梯分数   人均GDP   社会支持  健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法\n",
       "0      Finland  7.842  10.775  0.954    72.0      0.949 -0.098   0.186\n",
       "1      Denmark  7.620  10.933  0.954    72.7      0.946  0.030   0.179\n",
       "2  Switzerland  7.571  11.117  0.942    74.4      0.919  0.025   0.292\n",
       "3      Iceland  7.554  10.878  0.983    73.0      0.955  0.160   0.673\n",
       "4  Netherlands  7.464  10.932  0.942    72.4      0.913  0.175   0.338"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从原本11个指标提取8个指标\n",
    "data_2_提取 = data_2[['国家名称', '阶梯分数', '人均GDP', '社会支持', '健康预期寿命', '做出人生选择的自由', '慷慨', '对腐败的看法']]\n",
    "data_2_提取.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "b7066780-1da1-42b0-954f-1c742af20dac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "      <th>年</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Finland</td>\n",
       "      <td>7.842</td>\n",
       "      <td>10.775</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.000</td>\n",
       "      <td>0.949</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.186</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>7.620</td>\n",
       "      <td>10.933</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.700</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.179</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Switzerland</td>\n",
       "      <td>7.571</td>\n",
       "      <td>11.117</td>\n",
       "      <td>0.942</td>\n",
       "      <td>74.400</td>\n",
       "      <td>0.919</td>\n",
       "      <td>0.025</td>\n",
       "      <td>0.292</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Iceland</td>\n",
       "      <td>7.554</td>\n",
       "      <td>10.878</td>\n",
       "      <td>0.983</td>\n",
       "      <td>73.000</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0.160</td>\n",
       "      <td>0.673</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>7.464</td>\n",
       "      <td>10.932</td>\n",
       "      <td>0.942</td>\n",
       "      <td>72.400</td>\n",
       "      <td>0.913</td>\n",
       "      <td>0.175</td>\n",
       "      <td>0.338</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>Lesotho</td>\n",
       "      <td>3.512</td>\n",
       "      <td>7.926</td>\n",
       "      <td>0.787</td>\n",
       "      <td>48.700</td>\n",
       "      <td>0.715</td>\n",
       "      <td>-0.131</td>\n",
       "      <td>0.915</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>Botswana</td>\n",
       "      <td>3.467</td>\n",
       "      <td>9.782</td>\n",
       "      <td>0.784</td>\n",
       "      <td>59.269</td>\n",
       "      <td>0.824</td>\n",
       "      <td>-0.246</td>\n",
       "      <td>0.801</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>Rwanda</td>\n",
       "      <td>3.415</td>\n",
       "      <td>7.676</td>\n",
       "      <td>0.552</td>\n",
       "      <td>61.400</td>\n",
       "      <td>0.897</td>\n",
       "      <td>0.061</td>\n",
       "      <td>0.167</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>3.145</td>\n",
       "      <td>7.943</td>\n",
       "      <td>0.750</td>\n",
       "      <td>56.201</td>\n",
       "      <td>0.677</td>\n",
       "      <td>-0.047</td>\n",
       "      <td>0.821</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2.523</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.463</td>\n",
       "      <td>52.493</td>\n",
       "      <td>0.382</td>\n",
       "      <td>-0.102</td>\n",
       "      <td>0.924</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>149 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            国家名称   阶梯分数   人均GDP   社会支持  健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法     年\n",
       "0        Finland  7.842  10.775  0.954  72.000      0.949 -0.098   0.186  2021\n",
       "1        Denmark  7.620  10.933  0.954  72.700      0.946  0.030   0.179  2021\n",
       "2    Switzerland  7.571  11.117  0.942  74.400      0.919  0.025   0.292  2021\n",
       "3        Iceland  7.554  10.878  0.983  73.000      0.955  0.160   0.673  2021\n",
       "4    Netherlands  7.464  10.932  0.942  72.400      0.913  0.175   0.338  2021\n",
       "..           ...    ...     ...    ...     ...        ...    ...     ...   ...\n",
       "144      Lesotho  3.512   7.926  0.787  48.700      0.715 -0.131   0.915  2021\n",
       "145     Botswana  3.467   9.782  0.784  59.269      0.824 -0.246   0.801  2021\n",
       "146       Rwanda  3.415   7.676  0.552  61.400      0.897  0.061   0.167  2021\n",
       "147     Zimbabwe  3.145   7.943  0.750  56.201      0.677 -0.047   0.821  2021\n",
       "148  Afghanistan  2.523   7.695  0.463  52.493      0.382 -0.102   0.924  2021\n",
       "\n",
       "[149 rows x 9 columns]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_2_提取['年'] = 2021\n",
    "data_2_提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "fefb2f4e-37f2-479b-99b1-10960c861565",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "      <th>年</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>Lesotho</td>\n",
       "      <td>3.512</td>\n",
       "      <td>7.926</td>\n",
       "      <td>0.787</td>\n",
       "      <td>48.700</td>\n",
       "      <td>0.715</td>\n",
       "      <td>-0.131</td>\n",
       "      <td>0.915</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>Botswana</td>\n",
       "      <td>3.467</td>\n",
       "      <td>9.782</td>\n",
       "      <td>0.784</td>\n",
       "      <td>59.269</td>\n",
       "      <td>0.824</td>\n",
       "      <td>-0.246</td>\n",
       "      <td>0.801</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>Rwanda</td>\n",
       "      <td>3.415</td>\n",
       "      <td>7.676</td>\n",
       "      <td>0.552</td>\n",
       "      <td>61.400</td>\n",
       "      <td>0.897</td>\n",
       "      <td>0.061</td>\n",
       "      <td>0.167</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>3.145</td>\n",
       "      <td>7.943</td>\n",
       "      <td>0.750</td>\n",
       "      <td>56.201</td>\n",
       "      <td>0.677</td>\n",
       "      <td>-0.047</td>\n",
       "      <td>0.821</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2.523</td>\n",
       "      <td>7.695</td>\n",
       "      <td>0.463</td>\n",
       "      <td>52.493</td>\n",
       "      <td>0.382</td>\n",
       "      <td>-0.102</td>\n",
       "      <td>0.924</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            国家名称   阶梯分数  人均GDP   社会支持  健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法     年\n",
       "144      Lesotho  3.512  7.926  0.787  48.700      0.715 -0.131   0.915  2021\n",
       "145     Botswana  3.467  9.782  0.784  59.269      0.824 -0.246   0.801  2021\n",
       "146       Rwanda  3.415  7.676  0.552  61.400      0.897  0.061   0.167  2021\n",
       "147     Zimbabwe  3.145  7.943  0.750  56.201      0.677 -0.047   0.821  2021\n",
       "148  Afghanistan  2.523  7.695  0.463  52.493      0.382 -0.102   0.924  2021"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 步骤1: 从data_1中筛选出'年'列等于2021的行\n",
    "data_1_2021 = data_1[data_1['年'] == 2021]\n",
    "# 步骤2: 使用pandas.concat()将筛选出的数据与data_2_提取合并\n",
    "# 确保两个DataFrame的列名和顺序匹配\n",
    "data_use = pd.concat([data_2_提取, data_1_2021[data_2_提取.columns]], axis=0)\n",
    "# 查看合并后的数据\n",
    "data_use.tail() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "c4e07dc4-7e83-40de-acf6-a01a3eb6288c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 149 entries, 0 to 148\n",
      "Data columns (total 9 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   国家名称       149 non-null    object \n",
      " 1   阶梯分数       149 non-null    float64\n",
      " 2   人均GDP      149 non-null    float64\n",
      " 3   社会支持       149 non-null    float64\n",
      " 4   健康预期寿命     149 non-null    float64\n",
      " 5   做出人生选择的自由  149 non-null    float64\n",
      " 6   慷慨         149 non-null    float64\n",
      " 7   对腐败的看法     149 non-null    float64\n",
      " 8   年          149 non-null    int64  \n",
      "dtypes: float64(7), int64(1), object(1)\n",
      "memory usage: 11.6+ KB\n"
     ]
    }
   ],
   "source": [
    "data_use.info() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "53f8a7d7-4a30-4cc8-a98f-9ad52748cb3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>阶梯分数</th>\n",
       "      <th>人均GDP</th>\n",
       "      <th>社会支持</th>\n",
       "      <th>健康预期寿命</th>\n",
       "      <th>做出人生选择的自由</th>\n",
       "      <th>慷慨</th>\n",
       "      <th>对腐败的看法</th>\n",
       "      <th>年</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Finland</td>\n",
       "      <td>7.842</td>\n",
       "      <td>10.775</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.0</td>\n",
       "      <td>0.949</td>\n",
       "      <td>-0.098</td>\n",
       "      <td>0.186</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>7.620</td>\n",
       "      <td>10.933</td>\n",
       "      <td>0.954</td>\n",
       "      <td>72.7</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.179</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Switzerland</td>\n",
       "      <td>7.571</td>\n",
       "      <td>11.117</td>\n",
       "      <td>0.942</td>\n",
       "      <td>74.4</td>\n",
       "      <td>0.919</td>\n",
       "      <td>0.025</td>\n",
       "      <td>0.292</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Iceland</td>\n",
       "      <td>7.554</td>\n",
       "      <td>10.878</td>\n",
       "      <td>0.983</td>\n",
       "      <td>73.0</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0.160</td>\n",
       "      <td>0.673</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Netherlands</td>\n",
       "      <td>7.464</td>\n",
       "      <td>10.932</td>\n",
       "      <td>0.942</td>\n",
       "      <td>72.4</td>\n",
       "      <td>0.913</td>\n",
       "      <td>0.175</td>\n",
       "      <td>0.338</td>\n",
       "      <td>2021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          国家名称   阶梯分数   人均GDP   社会支持  健康预期寿命  做出人生选择的自由     慷慨  对腐败的看法     年\n",
       "0      Finland  7.842  10.775  0.954    72.0      0.949 -0.098   0.186  2021\n",
       "1      Denmark  7.620  10.933  0.954    72.7      0.946  0.030   0.179  2021\n",
       "2  Switzerland  7.571  11.117  0.942    74.4      0.919  0.025   0.292  2021\n",
       "3      Iceland  7.554  10.878  0.983    73.0      0.955  0.160   0.673  2021\n",
       "4  Netherlands  7.464  10.932  0.942    72.4      0.913  0.175   0.338  2021"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_use.head() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "01bb1317-bbef-43d9-9e9c-4900073f6e7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 149 entries, 0 to 148\n",
      "Data columns (total 9 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   国家名称       149 non-null    object \n",
      " 1   阶梯分数       149 non-null    float64\n",
      " 2   人均GDP      149 non-null    float64\n",
      " 3   社会支持       149 non-null    float64\n",
      " 4   健康预期寿命     149 non-null    float64\n",
      " 5   做出人生选择的自由  149 non-null    float64\n",
      " 6   慷慨         149 non-null    float64\n",
      " 7   对腐败的看法     149 non-null    float64\n",
      " 8   年          149 non-null    int64  \n",
      "dtypes: float64(7), int64(1), object(1)\n",
      "memory usage: 11.6+ KB\n"
     ]
    }
   ],
   "source": [
    "# 使用平均值填充\n",
    "for i in data_use.iloc[:, 3:].columns:\n",
    "    data_use[i] = data_use[i].fillna(data_use[i].mean())\n",
    "data_use.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "49323945-459f-4270-a3d2-f55bcbdcd02c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pyecharts in c:\\anaconda\\lib\\site-packages (2.0.7)\n",
      "Requirement already satisfied: jinja2 in c:\\anaconda\\lib\\site-packages (from pyecharts) (3.1.4)\n",
      "Requirement already satisfied: prettytable in c:\\anaconda\\lib\\site-packages (from pyecharts) (3.12.0)\n",
      "Requirement already satisfied: simplejson in c:\\anaconda\\lib\\site-packages (from pyecharts) (3.19.3)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in c:\\anaconda\\lib\\site-packages (from jinja2->pyecharts) (2.1.3)\n",
      "Requirement already satisfied: wcwidth in c:\\anaconda\\lib\\site-packages (from prettytable->pyecharts) (0.2.5)\n"
     ]
    }
   ],
   "source": [
    "!pip install pyecharts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "4cf54796-cb18-47d3-89fb-6a028593c9fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\折线图.html'"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Line\n",
    "from pyecharts.charts import Bar\n",
    "import pyecharts.options as opts\n",
    "%matplotlib inline\n",
    "from IPython.display import display\n",
    "\n",
    "# 计算每年的平均幸福感，只对数值类型的列进行计算\n",
    "data_平均 = data_1.groupby(by='年').mean(numeric_only=True).reset_index()\n",
    "\n",
    "# 绘制折线图\n",
    "c = (\n",
    "    Line()\n",
    "    .add_xaxis(list(data_平均.iloc[:, 0].astype('str')))\n",
    "    .set_global_opts(\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"axis\"),\n",
    "    )\n",
    ")\n",
    "for i in data_平均.columns[1:2]:\n",
    "    c.add_yaxis(i, list(data_平均[i].round(3)))\n",
    "c.render(\"折线图.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "7bef24e0-06e0-44fd-8551-76383c8a3494",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.9398160157977792, 0.8210214796868415, 0.7789446921541844, 0.663117493185772, 0.8262088014657104, -0.5840197839970248, 0.8436973404939468, 0.05535643384968948]\n"
     ]
    }
   ],
   "source": [
    "#初始化列表时常用A=[]\n",
    "#.corr（）\n",
    "#pearson:相关系数：来衡量两个数据集合是否在一条线上面，即针对线性数据的相关系数计算，针对非线性数据便会有误差。\n",
    "#print(data_平均.columns[2:])  第二列到最后一列的列名\n",
    "#print(data_平均.iloc[:,1])   所有行的第一列\n",
    "A=[]\n",
    "for i in data_平均.columns[2:]:\n",
    "     a=data_平均.iloc[:,1].corr(data_平均[i],method=\"pearson\")\n",
    "     A.append(a)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "f0638979-db41-43e8-92f4-201a335fbd4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\幸福感与X的相关系数图.html'"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Bar()绘制柱状图。\n",
    "c=(\n",
    "    Bar()\n",
    "    .add_xaxis(list(data_平均.columns[2:]))\n",
    "    .add_yaxis('幸福感与X的相关系数',A)\n",
    ")\n",
    "c.render(\"幸福感与X的相关系数图.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "9b2678b8-1631-48ef-bcb8-defe3eb5ae54",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pyecharts制作平行坐标系。\n",
    "from pyecharts.charts import Parallel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "b110a81b-b495-4476-9c87-4166adf9c281",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]\n"
     ]
    }
   ],
   "source": [
    "#list()用于创建列表,.count()统计在字符串/列表/元组中某个字符出现的次数,.index表示索引\n",
    "Time=list(data_1.groupby(by='年').count().index)\n",
    "print(Time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "08357b56-ef61-4c83-b3a7-57926f0ac294",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>2005</th>\n",
       "      <th>2006</th>\n",
       "      <th>2007</th>\n",
       "      <th>2008</th>\n",
       "      <th>2009</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "      <th>2020</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Albania</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.485</td>\n",
       "      <td>5.269</td>\n",
       "      <td>5.867</td>\n",
       "      <td>5.510</td>\n",
       "      <td>4.551</td>\n",
       "      <td>4.814</td>\n",
       "      <td>4.607</td>\n",
       "      <td>4.511</td>\n",
       "      <td>4.640</td>\n",
       "      <td>5.004</td>\n",
       "      <td>4.995</td>\n",
       "      <td>5.365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Argentina</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.313</td>\n",
       "      <td>6.073</td>\n",
       "      <td>5.961</td>\n",
       "      <td>6.424</td>\n",
       "      <td>6.441</td>\n",
       "      <td>6.776</td>\n",
       "      <td>6.468</td>\n",
       "      <td>6.582</td>\n",
       "      <td>6.671</td>\n",
       "      <td>6.697</td>\n",
       "      <td>6.427</td>\n",
       "      <td>6.039</td>\n",
       "      <td>5.793</td>\n",
       "      <td>6.086</td>\n",
       "      <td>5.901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Australia</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.450</td>\n",
       "      <td>7.406</td>\n",
       "      <td>7.196</td>\n",
       "      <td>7.364</td>\n",
       "      <td>7.289</td>\n",
       "      <td>7.309</td>\n",
       "      <td>7.250</td>\n",
       "      <td>7.257</td>\n",
       "      <td>7.177</td>\n",
       "      <td>7.234</td>\n",
       "      <td>7.137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Austria</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.303</td>\n",
       "      <td>7.471</td>\n",
       "      <td>7.401</td>\n",
       "      <td>7.499</td>\n",
       "      <td>6.950</td>\n",
       "      <td>7.076</td>\n",
       "      <td>7.048</td>\n",
       "      <td>7.294</td>\n",
       "      <td>7.396</td>\n",
       "      <td>7.195</td>\n",
       "      <td>7.213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Bahrain</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.098</td>\n",
       "      <td>6.173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>United States</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.182</td>\n",
       "      <td>7.513</td>\n",
       "      <td>7.280</td>\n",
       "      <td>7.158</td>\n",
       "      <td>7.164</td>\n",
       "      <td>7.115</td>\n",
       "      <td>7.026</td>\n",
       "      <td>7.249</td>\n",
       "      <td>7.151</td>\n",
       "      <td>6.864</td>\n",
       "      <td>6.804</td>\n",
       "      <td>6.992</td>\n",
       "      <td>6.883</td>\n",
       "      <td>6.944</td>\n",
       "      <td>7.028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>Uruguay</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.786</td>\n",
       "      <td>5.694</td>\n",
       "      <td>5.664</td>\n",
       "      <td>6.296</td>\n",
       "      <td>6.062</td>\n",
       "      <td>6.554</td>\n",
       "      <td>6.450</td>\n",
       "      <td>6.444</td>\n",
       "      <td>6.561</td>\n",
       "      <td>6.628</td>\n",
       "      <td>6.171</td>\n",
       "      <td>6.336</td>\n",
       "      <td>6.372</td>\n",
       "      <td>6.600</td>\n",
       "      <td>6.310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Venezuela</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.258</td>\n",
       "      <td>7.189</td>\n",
       "      <td>7.478</td>\n",
       "      <td>6.580</td>\n",
       "      <td>7.067</td>\n",
       "      <td>6.553</td>\n",
       "      <td>6.136</td>\n",
       "      <td>5.569</td>\n",
       "      <td>4.041</td>\n",
       "      <td>5.071</td>\n",
       "      <td>5.006</td>\n",
       "      <td>5.081</td>\n",
       "      <td>4.574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>Zambia</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.999</td>\n",
       "      <td>5.013</td>\n",
       "      <td>5.244</td>\n",
       "      <td>4.346</td>\n",
       "      <td>4.843</td>\n",
       "      <td>4.348</td>\n",
       "      <td>3.933</td>\n",
       "      <td>4.041</td>\n",
       "      <td>3.307</td>\n",
       "      <td>4.838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.826</td>\n",
       "      <td>3.280</td>\n",
       "      <td>3.174</td>\n",
       "      <td>4.056</td>\n",
       "      <td>4.682</td>\n",
       "      <td>4.846</td>\n",
       "      <td>4.955</td>\n",
       "      <td>4.690</td>\n",
       "      <td>4.184</td>\n",
       "      <td>3.703</td>\n",
       "      <td>3.735</td>\n",
       "      <td>3.638</td>\n",
       "      <td>3.616</td>\n",
       "      <td>2.694</td>\n",
       "      <td>3.160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>95 rows × 17 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             国家名称  2005   2006   2007   2008   2009   2010   2011   2012  \\\n",
       "0         Albania   NaN    NaN    NaN    NaN  5.485  5.269  5.867  5.510   \n",
       "1       Argentina   NaN  6.313  6.073  5.961  6.424  6.441  6.776  6.468   \n",
       "2       Australia   NaN    NaN    NaN    NaN    NaN  7.450  7.406  7.196   \n",
       "3         Austria   NaN    NaN    NaN    NaN    NaN  7.303  7.471  7.401   \n",
       "4         Bahrain   NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN   \n",
       "..            ...   ...    ...    ...    ...    ...    ...    ...    ...   \n",
       "90  United States   NaN  7.182  7.513  7.280  7.158  7.164  7.115  7.026   \n",
       "91        Uruguay   NaN  5.786  5.694  5.664  6.296  6.062  6.554  6.450   \n",
       "92      Venezuela   NaN    NaN    NaN  6.258  7.189  7.478  6.580  7.067   \n",
       "93         Zambia   NaN    NaN    NaN    NaN    NaN    NaN  4.999  5.013   \n",
       "94       Zimbabwe   NaN  3.826  3.280  3.174  4.056  4.682  4.846  4.955   \n",
       "\n",
       "     2013   2014   2015   2016   2017   2018   2019   2020  \n",
       "0   4.551  4.814  4.607  4.511  4.640  5.004  4.995  5.365  \n",
       "1   6.582  6.671  6.697  6.427  6.039  5.793  6.086  5.901  \n",
       "2   7.364  7.289  7.309  7.250  7.257  7.177  7.234  7.137  \n",
       "3   7.499  6.950  7.076  7.048  7.294  7.396  7.195  7.213  \n",
       "4     NaN    NaN    NaN    NaN    NaN    NaN  7.098  6.173  \n",
       "..    ...    ...    ...    ...    ...    ...    ...    ...  \n",
       "90  7.249  7.151  6.864  6.804  6.992  6.883  6.944  7.028  \n",
       "91  6.444  6.561  6.628  6.171  6.336  6.372  6.600  6.310  \n",
       "92  6.553  6.136  5.569  4.041  5.071  5.006  5.081  4.574  \n",
       "93  5.244  4.346  4.843  4.348  3.933  4.041  3.307  4.838  \n",
       "94  4.690  4.184  3.703  3.735  3.638  3.616  2.694  3.160  \n",
       "\n",
       "[95 rows x 17 columns]"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_国家幸福感=data_1[data_1['年']==Time[0]][['国家名称','阶梯分数']]\n",
    "data_国家幸福感.columns=['国家名称',Time[0]]\n",
    "data_国家幸福感=data_国家幸福感.sort_values('国家名称')\n",
    "for i in Time[1:]:\n",
    "    a=data_1[data_1['年']==i][['国家名称','阶梯分数']]\n",
    "    a.columns=['国家名称',i]\n",
    "    a=a.sort_values('国家名称')\n",
    "    data_国家幸福感=data_国家幸福感.merge(a,on='国家名称',how='right')\n",
    "data_国家幸福感=data_国家幸福感.round(3)\n",
    "data_国家幸福感"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "1cea38a0-f974-4c7e-b752-00831ec23fea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家名称</th>\n",
       "      <th>2020</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Albania</td>\n",
       "      <td>5.365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>Argentina</td>\n",
       "      <td>5.901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>Australia</td>\n",
       "      <td>7.137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Austria</td>\n",
       "      <td>7.213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>Bahrain</td>\n",
       "      <td>6.173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1850</th>\n",
       "      <td>United States</td>\n",
       "      <td>7.028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1865</th>\n",
       "      <td>Uruguay</td>\n",
       "      <td>6.310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1893</th>\n",
       "      <td>Venezuela</td>\n",
       "      <td>4.574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1933</th>\n",
       "      <td>Zambia</td>\n",
       "      <td>4.838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1948</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>3.160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>95 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               国家名称   2020\n",
       "24          Albania  5.365\n",
       "51        Argentina  5.901\n",
       "79        Australia  7.137\n",
       "92          Austria  7.213\n",
       "117         Bahrain  6.173\n",
       "...             ...    ...\n",
       "1850  United States  7.028\n",
       "1865        Uruguay  6.310\n",
       "1893      Venezuela  4.574\n",
       "1933         Zambia  4.838\n",
       "1948       Zimbabwe  3.160\n",
       "\n",
       "[95 rows x 2 columns]"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#def 函数名(参数1，参数2，参数…)：\n",
    "#  函数体（语句块）\n",
    "#  return [返回值]\n",
    "def 信息(i,name,data):\n",
    "    a={\n",
    "        \"dim\": i,\n",
    "        \"name\": name,\n",
    "        \"type\": \"category\",\"data\": list(data)\n",
    "    }\n",
    "    return a\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "37f80f7e-7607-4c32-87b0-246441d7ce38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\死亡公司属性.html'"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parallel_axis = [\n",
    "    信息(0,'国家名称',data_国家幸福感['国家名称']),\n",
    "]\n",
    "for i in range(len(Time)):\n",
    "    parallel_axis.append(信息(i+1,Time[i],list(data_国家幸福感.groupby(by=Time[i]).count().index)),)\n",
    "\n",
    "c=(\n",
    "    Parallel(init_opts=opts.InitOpts(width=\"1400px\", height=\"800px\"))\n",
    "  .add_schema(schema=parallel_axis)\n",
    "  .add(\n",
    "        series_name=\"死亡公司属性\",\n",
    "        data=[list(z) for z in zip(data_国家幸福感['国家名称'],data_国家幸福感[2005],data_国家幸福感[2006],data_国家幸福感[2007],data_国家幸福感[2008],\n",
    "        data_国家幸福感[2009],data_国家幸福感[2010],data_国家幸福感[2011],data_国家幸福感[2012],data_国家幸福感[2013],data_国家幸福感[2014],data_国家幸福感[2015],\n",
    "        data_国家幸福感[2016],data_国家幸福感[2017],data_国家幸福感[2018],data_国家幸福感[2019],data_国家幸福感[2020],\n",
    "        )],\n",
    "        linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.2),\n",
    "    )\n",
    ")\n",
    "c.render(\"死亡公司属性.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fbc17aa-9704-424f-bb7c-465431982a2f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
